package com.zerdoor.service.impl;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.zerdoor.dao.RbacRoleDao;
import com.zerdoor.service.RbacRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by xiaoji on 2015/2/2.
 */
@Service
public class RbacRoleServiceImpl implements RbacRoleService{
    @Autowired
    private RbacRoleDao rbacRoleDao;

    /**
     * 角色列表
     *
     * @param map
     * @return
     */
    @Override
    public List<Map<String, Object>> getRbacRole(Map<String, Object> map) {
        return rbacRoleDao.getRbacRole(map);
    }

    /**
     * 添加角色
     *
     * @param map
     * @throws Exception
     */
    @Override
    public void addRbacRole(Map<String, Object> map) throws Exception {
        rbacRoleDao.addRbacRole(map);
    }

    /**
     * 添加角色资源操作
     *
     * @param map
     * @throws Exception
     */
    @Override
    public void addrro(Map<String, Object> map) throws Exception {
        rbacRoleDao.addrro(map);
    }

    /**
     * 删除角色资源操作
     *
     * @param map
     * @throws Exception
     */
    @Override
    public void deleterro(Map<String, Object> map) throws Exception {
        rbacRoleDao.deleterro(map);
    }

    /**
     * 编辑角色
     *
     * @param map
     * @throws Exception
     */
    @Override
    public void updateRbacRole(Map<String, Object> map) throws Exception {
        rbacRoleDao.updateRbacRole(map);
    }

    /**
     * 删除角色
     *
     * @param roleId 角色编号
     * @throws Exception
     */
    @Override
    public void deleteRbacRole(int roleId) throws Exception {
        rbacRoleDao.deleteRbacRole(roleId);
    }

    /**
     * 获取资源
     *
     * @return
     */
    @Override
    public List<Map<String, Object>> getResources() {
        return rbacRoleDao.getResources();
    }

    /**
     * 设置权限资源时初始化操作码值
     *
     * @param roleId
     * @return
     */
    @Override
    public List<Map<String, Object>> getOpIdsByRoleId(int roleId) {
        return rbacRoleDao.getOpIdsByRoleId(roleId);
    }

    /**
     * 获取操作码
     *
     * @return
     */
    @Override
    public List<Map<String, Object>> getOperatings() {
        return rbacRoleDao.getOperatings();
    }

    /**
     * 激活角色
     *
     * @param map
     */
    @Override
    public void updateActivationRbacRole(Map<String, Object> map) throws Exception {
        rbacRoleDao.updateActivationRbacRole(map);
    }

    /**
     * 停用角色
     *
     * @param map
     */
    @Override
    public void updateStopRbacRole(Map<String, Object> map) throws Exception {
        rbacRoleDao.updateStopRbacRole(map);
    }

    /**
     * 给角色付资源权限
     *
     * @param roleId       角色编号
     * @param arr 权限
     */
    @Override
    public void updateRro(int roleId,JsonArray arr) throws Exception{

        if(arr.size() > 0){
            //删除该角色没有的资源权限
            Map<String,Object> map = new HashMap<String, Object>();
            map.put("roleId",roleId);
            //循环遍历权限
            String resourcesIds = "";
            for(int i=0;i<arr.size();i++){
                JsonObject obj = (JsonObject)arr.get(i);
                if(i!=0){
                    resourcesIds +=",";
                }
                //资源编号
                resourcesIds += obj.get("id").getAsString();
                map.put("resourcesId",obj.get("id").getAsInt());

                String opIds = obj.get("opIds").getAsString();
                if(opIds != null && opIds!= ""){
                    String[] arrop = opIds.split(",");
                    for(int j=0;j<arrop.length;j++){
                        Map<String,Object> temp = new HashMap<String, Object>();
                        temp.put("roleId",roleId);
                        temp.put("resourcesId",obj.get("id").getAsInt());
                        temp.put("opId",Integer.parseInt(arrop[j]));
                        temp.put("isValid",101001);
                        //是否存在角色资源操作码
                        Map<String, Object> total  = rbacRoleDao.getRroByrroId(temp);
                        if(Integer.parseInt(total.get("total").toString()) == 0){//该角色不存在该权限时候添加权限
                            rbacRoleDao.addrro(temp);
                        }
                    }
                    //删除去掉的操作码
                    map.put("opIds",opIds);
                    rbacRoleDao.deleterro2(map);
                }
            }
            map.put("resourcesIds",resourcesIds);
            //删除去掉的资源
            rbacRoleDao.deleterro(map);

        }
    }
}
